import json

from utils.readAndWriteBase import Book, Note


class Tower(Book):
    def __init__(self, bookName='book.xls'):
        Book.__init__(self, bookName)

    def get_GJ_info_all(self):
        content = list()
        for i in range(0, len(self.get_GJ_name())):
            con = self.get_GJ_info(i)
            content.append(json.dumps(con))
        return content

    def get_GJ_info_all_jc(self):
        content = list()
        for i in range(0, len(self.get_GJ_name())):
            con = self.get_GJ_info_jc(i)
            content.append(json.dumps(con))
        return content

    def read(self):
        content = self.get_GJ_name()
        print(content)

    # --------- 工具 ------------
    def get_GJ_info(self, index):
        index2 = 2 * (index + 1) - 2
        info = dict()
        info['type'] = self.get_GJ_col(1)[index]
        info['seg'] = self.get_GJ_col(0)[index2]
        info['num'] = self.get_GJ_col(2)[index]
        info['spec'] = list()
        spec_info = dict()
        spec_info['name'] = self.get_GJ_col(3)[index2]
        spec_info['design'] = self.get_GJ_col(7)[index2]
        spec_info['range'] = self.get_GJ_col(9)[index2]
        info['spec'].append(spec_info)
        spec_info = dict()
        spec_info['name'] = self.get_GJ_col(3)[index2 + 1]
        spec_info['design'] = self.get_GJ_col(7)[index2 + 1]
        spec_info['range'] = self.get_GJ_col(9)[index2 + 1]
        info['spec'].append(spec_info)
        return info

    def get_GJ_info_jc(self, index):
        """
        构件检测信息
        :param index:
        :return:
        """
        index2 = 2 * (index + 1) - 2
        info = self.get_GJ_info(index)
        spec_info = info['spec']
        spec_info[0]['measured'] = self.get_GJ_col(9)[index2]
        spec_info[0]['qualified'] = self.get_GJ_col(10)[index2]
        spec_info[1]['measured'] = self.get_GJ_col(9)[index2 + 1]
        spec_info[1]['qualified'] = self.get_GJ_col(10)[index2 + 1]
        info['spec'] = spec_info
        return info

    def get_GJ_col(self, num):
        """
        获取构件各种属性
        :param num:
        :return:
        """
        content = self.read_col(num)[0][2:]
        if num == 1 or num == 2:
            return content[::2]
        else:
            return content

    def get_GJ_name(self):
        """
        获取构件名
        :return:
        """
        knowlege = list()
        contain = self.mix_col(1, 0, 2, label='-')
        for con in contain:
            if con[0] == '-':
                continue
            else:
                knowlege.append(con)
        return knowlege[1:]


class TowerNote(Note):
    def __init__(self):
        Note.__init__(self)


if __name__ == '__main__':
    tower = Tower()
    name = tower.get_GJ_name()
    info = tower.get_GJ_info_all()
    info_jc = tower.get_GJ_info_all_jc()
    con = info_jc[0]
    print(json.loads(con))
    # towern = TowerNote()
    # towern.write_col(0, name)
    # towern.write_col(1, info)
    # towern.write_col(2, info_jc)
    # towern.save()
